package com.imessage.service.impl;

import com.bean.ManageResource;
import com.bean.ManageRole;
import com.imessage.service.BaseService;
import com.imessage.service.RoleMenuService;
import io.ebean.SqlQuery;
import io.ebean.SqlRow;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class RoleMenuServiceImpl implements RoleMenuService {

    @Autowired
    private BaseService baseService;

    @Override
    public List<ManageResource> findManageMenuList(Long manage_role_id) {
        StringBuffer sqlsb=new StringBuffer();
        sqlsb.append("select manage_menu.* from manage_resource_role as manage_role_menu");
        sqlsb.append(" left join manage_resource as manage_menu on manage_menu.id= manage_role_menu.manage_resource_id");
        sqlsb.append(" where manage_role_menu.manage_role_id =:manage_role_id");
        SqlQuery sqlQuery =baseService.createSqlQuery(sqlsb.toString());
        sqlQuery.setParameter("manage_role_id", manage_role_id);
        List<SqlRow> sqlRowList=sqlQuery.findList();
        List<ManageResource> ManageResourceList=new ArrayList<ManageResource>();
        if(sqlRowList==null||sqlRowList.size()==0){
            return ManageResourceList;
        }
        for(SqlRow sqlRow:sqlRowList){
            ManageResource ManageResource=new ManageResource();
            ManageResource.setName(sqlRow.getString("name"));
            ManageResourceList.add(ManageResource);
        }
        return ManageResourceList;
    }


    @Override
    public List<ManageRole> findManageRoleList(Long manage_user_id) {
        StringBuffer sqlsb=new StringBuffer();
        sqlsb.append("select manage_role.* from manage_role_user as manage_user_role");
        sqlsb.append(" left join manage_role as manage_role on manage_role.id= manage_user_role.manage_role_id");
        sqlsb.append(" where manage_user_role.manage_user_id =:manage_user_id");
        SqlQuery sqlQuery =baseService.createSqlQuery(sqlsb.toString());
        sqlQuery.setParameter("manage_user_id", manage_user_id);
        List<SqlRow> sqlRowList=sqlQuery.findList();
        List<ManageRole> manageRoleList=new ArrayList<ManageRole>();
        if(sqlRowList==null||sqlRowList.size()==0){
            return manageRoleList;
        }
        for(SqlRow sqlRow:sqlRowList){
            ManageRole manageRole=new ManageRole();
            manageRole.setName(sqlRow.getString("name"));
            manageRoleList.add(manageRole);
        }
        return manageRoleList;
    }
}
